<?php
/*
 * index控制器  打开主页位置
 * Tobey
 * */
require_once __ROOT__."/tayphp/Controller.class.php";
class indexController extends Controller{
	/*
	 *打开主页
	 * */
	public function index(){
		if(IS_GET) {
            if(IS_PC){
                $this->display("Index/index");
            } else{
                $this->redirect("http://121.42.185.80:8081/");
            }
        } else if (IS_MOBILE) {
            $this->redirect("http://121.42.185.80:8081/");
        }
    }

	/*
	 * 打开登录页面 GET
	 * 登录处理  ajax请求  本地域名请求 
     * 参数  邮箱 密码 身份
	 * */
	public function login() {
		if(IS_GET) {
            session_start();
            if (!isset($_SESSION["name"]) || empty($_SESSION["name"])) {      //没有session['name']这个信息表示没有登陆状态
                $this->display("Account/login");          					 //所以跳转到登陆页面
            } else {
                $this->redirect("index/index");      //有登陆信息的话就直接去主页面..
            }
            session_write_close();
        } else if(IS_AJAX && IS_LOCAL){
            $sql = D();
            $state = "";
            if($sql->table(array("teacher"=>""))->where(array("tea_email"=>$_POST["number"]))->count()){                      //教师登陆
                //获取教师的基本信息   存放在$teacher变量中
                $teacher = $sql->table(array("teacher"=>""))->where(array("tea_email"=>$_POST["number"]))->field(array("tea_name","tea_email","tea_id","tea_password_hash"))->find();                    
                if ($teacher && $teacher["tea_password_hash"] == md5($_POST["password"])) {      //判断登录的教师是否存在还有密码是否正确
                 session_start();
                 $_SESSION["name"] = $teacher['tea_name'];
                 $_SESSION['number'] = $teacher["tea_email"];
                 $_SESSION["id"] = $teacher["tea_id"];
                 $_SESSION["isTeacher"] = "true";
                 $state = "true";
                 session_write_close();
                 $data = $this->message(1,"登录验证通过!",array("name"=>$teacher['tea_name']));
             } else {
                $data = $this->message(0,"帐号或者密码错误!","");
            }
            unset($teacher);
        }
            else if($sql->table(array("student"=>""))->where(array("stu_email"=>$_POST["number"]))->count()){                                       //学生登陆
                //获取登录学生的基本信息  存放$student变量中
                $student = $sql->table(array("student"=>""))->where(array("stu_email"=>$_POST["number"]))->field(array("stu_name", "stu_password_hash", "stu_email", "stu_id"))->find();
                if ($student && $student["stu_password_hash"] == md5($_POST["password"])) {     //验证登录是否通过
                 session_start();
                 $_SESSION["name"] = $student['stu_name'];
                 $_SESSION['number'] = $student["stu_email"];
                 $_SESSION["id"] = $student["stu_id"];
                 $_SESSION["isTeacher"] = "false";
                 session_write_close();
                 $state = "false";
                 $data = $this->message(1,"登录验证通过!",array("name"=>$student['stu_name']));
             } else {
                $data = $this->message(0,"帐号或者密码错误","");
            }
            unset($student);
        } else{
            $data = $this->message(0,"帐号或者密码错误!","");
        }
        if ($data["status"] == 1) {
                if (isset($_POST["remember"]) && $_POST["remember"]) {    // 记住登录状态
                    $remember = 604800;
                } else {
                    $remember = 86400;
                }
                session_start();
                setcookie("name", $_SESSION["name"], time() + $remember,"/");
                session_write_close();
                setcookie("id", $_POST["number"], time() + $remember,"/");
                setcookie("pwd", $_POST["password"], time() + $remember,"/");
                setcookie("isTeacher", $state, time() + $remember,"/");
            }
            $this->ajaxReturn($data);
            unset($sql,$data);
        }
    }

   /**
    * 获取登录者的基本信息
    * ajax
    */
   public function getLoginInfo(){
    session_start();
    if(IS_AJAX && IS_LOCAL && isset($_SESSION["id"])){
        $sql = D();
        if($_SESSION['isTeacher'] == "true") {
            $info = $sql->table(array("teacher"=>"tea","college"=>"col"))->where(array("tea_id"=>$_SESSION["id"],"__string__"=>"tea.col_id = col.col_id"))->field(array("tea.tea_id", "tea.tea_number", "tea.tea_name", "tea.tea_email", "tea.sex", "col.col_name"))->find();
            if($info){
                $info["state"] = "教师";
                $data = $this->message(1,"获取信息成功!",$info);
            } else{
                $data = $this->message(0,"非法操作!!","");
            }
            unset($info);
        } else{
            $info = $sql->table(array("student"=>"stu","college"=>"col"))->where(array("stu_id"=>$_SESSION["id"],"__string__"=>"stu.col_id = col.col_id"))->field(array("stu.stu_id", "stu.stu_number", "stu.stu_name", "stu.stu_email", "stu.sex", "col.col_name"))->find();
            if($info){
                $info["state"] = "学生";
                $data = $this->message(1,"获取信息成功!",$info);
            } else{
                $data = $this->message(0,"非法操作!!","");
            }
            unset($info);
        }
        $this->ajaxReturn($data);
        unset($data);
    }
    session_write_close();
}

    /*
     * 登出   安全退出系统
     * */
    public function logout() {
        session_start();
        session_destroy();
        session_write_close();
        setcookie("name", "", time()-1,"/");
        //setcookie("id", "", time()-1,"/");
        setcookie("pwd", "", time()-1,"/");
        setcookie("isTeacher", "", time()-1,"/");
        if(IS_PC){
            $this->redirect("Account/login");
        } else{
            $this->ajaxReturn($this->message(-2,"退出成功!",""));
        }
        exit();
    }

    /*
     * 注册跳转,向注册页面返回所有学校的基本信息
     * */
    public function register(){
    	if(IS_GET) {
    		$this->display();
    	}
    }


    /*
     * 注册时,检测一下注册的email是否已经存在
     * ajax
     * */
    public function check_email(){                  //教师注册和学生注册通用的...post一个"email"..
    if(IS_AJAX && IS_LOCAL){
        $sql = D();
            if($sql->table(array("student"=>""))->where(array("stu_email"=>$_POST["email"]))->count() == 0 && $sql->table(array("teacher"=>""))->where(array("tea_email"=>$_POST["email"]))->count() == 0){   //两个表中检测email
             $data = $this->message(1,"邮箱可以进行注册","");
         } else{
             $data = $this->message(0,"邮箱已经被注册!","");
         }
         $this->ajaxReturn($data);
         unset($sql,$data);
     }
 }

    /*
     * 展示登陆者的信息
     * GET
     * */
    public function info(){
        if(IS_GET) {
            $this->display();
        }
    }

    /*
     * 跳转到修改密码的界面
     * GET
     * */
    public function change_pwd(){
        if(IS_GET) {
            $this->display();
        }
    }


    /**
     * 测试使用  
     */
    public function a(){
     $_POST["number"] = "ww@qq.com";
     $_POST["password"] = "ww";
     $sql = D();
            if($sql->table(array("teacher"=>""))->where(array("tea_email"=>$_POST["number"]))->count()){                      //教师登陆
                //获取教师的基本信息   存放在$teacher变量中
                $teacher = $sql->table(array("teacher"=>""))->where(array("tea_email"=>$_POST["number"]))->field(array("tea_name","tea_email","tea_id","tea_password_hash"))->find();                    
                if ($teacher && $teacher["tea_password_hash"] == md5($_POST["password"])) {      //判断登录的教师是否存在还有密码是否正确
                    session_start();
                    $_SESSION["name"] = $teacher['tea_name'];
                    $_SESSION['number'] = $teacher["tea_email"];
                    $_SESSION["id"] = $teacher["tea_id"];
                    $_SESSION["isTeacher"] = "true";
                    $data = $this->message(1,"登录验证通过!",array("name"=>$teacher['tea_name']));
                    session_write_close();
                } else {
                    $data = $this->message(0,"帐号或者密码错误!","");
                }
                unset($teacher);
            }
            else if($sql->table(array("student"=>""))->where(array("stu_email"=>$_POST["number"]))->count()){                                       //学生登陆
                //获取登录学生的基本信息  存放$student变量中
                $student = $sql->table(array("student"=>""))->where(array("stu_email"=>$_POST["number"]))->field(array("stu_name", "stu_password_hash", "stu_email", "stu_id"))->find();
                if ($student && $student["stu_password_hash"] == md5($_POST["password"])) {     //验证登录是否通过
                    session_start();
                    $_SESSION["name"] = $student['stu_name'];
                    $_SESSION['number'] = $student["stu_email"];
                    $_SESSION["id"] = $student["stu_id"];
                    $_SESSION["isTeacher"] = "false";
                    $data = $this->message(1,"登录验证通过!",array("name"=>$student['stu_name']));
                    session_write_close();
                } else {
                    $data = $this->message(0,"帐号或者密码错误","");
                }
                unset($student);
            } else{
                $data = $this->message(0,"帐号或者密码错误!","");
            }
            if ($data["status"] == 1) {
                if (isset($_POST["remember"]) && $_POST["remember"]) {    // 记住登录状态
                    $remember = 604800;
                } else {
                    $remember = 86400;
                }
                session_start();
                setcookie("name", $_SESSION["name"], time() + $remember);
                session_write_close();
                setcookie("id", $_POST["number"], time() + $remember);
                setcookie("pwd", $_POST["password"], time() + $remember);
            }
            $this->ajaxReturn($data);
            var_dump($data);
        }

    //析构函数不用写任何内容也是可以的
        public function __destruct(){}
    }